import {findItemsByUserId} from '~/server/database/repositories/itemOperation'
import {getUserFromJwt} from '~/utils/serveCommon'
export default defineEventHandler( async(context) => {
  try{
    const paramData = getQuery(context) || {}
    const { page, happened_after, happened_before} = paramData
    if(!page || !happened_after || !happened_before){
      return sendError(context,createError({
        statusCode: 400,
        statusMessage: '参数错误!',
      }))
    }else{
      if(isNaN(parseInt(page+''))){
        return sendError(context,createError({
          statusCode: 400,
          statusMessage: '参数类型错误!',
        }))
      }
    }

    const user_id = getUserFromJwt(context)
    const realPage = + page
    return await findItemsByUserId(user_id, realPage, happened_after as string, happened_before as string)
  }
  catch(error){
    return sendError(context, createError({
      statusCode: 400,
      statusMessage: '获取数据失败!',
    }))
  }
})